<?php
/**
 *   駿濤 （xjt@ourstu.com）
 */
namespace Pingxx\Controller;

use Think\Controller;


class CallPingxxController extends Controller
{


    public function pay()
    {
        require_once(dirname(__FILE__) . '/../Lib/Pingxx/init.php');


        $orderNo = I('post.order_id');

        $link = D('order_link')->where(array('order_id' => $orderNo))->find();
        $data = R(ucfirst($link['app']) . '/Pingxx/getParams', array('order_id' => $orderNo), 'Widget');

        $channel = $data['channel'];
        $subject = $data['subject'];
        $body = $data['body'];
        $amount = $data['amount'];


        //$extra 在使用某些渠道的时候，需要填入相应的参数，其它渠道则是 array() .具体见以下代码或者官网中的文档。其他渠道时可以传空值也可以不传。
        $extra = array();

        // $redirect_url = U('pingxx/pingxx1/payCallback','',true,true);
        $redirect_url = 'http://' . str_replace('//', '/', $_SERVER['HTTP_HOST'] . __ROOT__ . '/pingxx/callback') . '.php';

        switch ($channel) {
            case 'alipay_pc_direct':
                $extra = array(
                    'success_url' => $redirect_url,
                );
                break;

            case 'alipay_wap':
                $extra = array(
                    'success_url' => $redirect_url,
                    'cancel_url' => $redirect_url
                );
                break;
            case 'upmp_wap':
                $extra = array(
                    'result_url' => $redirect_url
                );
                break;
            case 'bfb_wap':
                $extra = array(
                    'result_url' => $redirect_url,
                    'bfb_login' => true
                );
                break;
            case 'upacp_wap':
                $extra = array(
                    'result_url' => $redirect_url
                );
                break;
            case 'wx_pub':
                $extra = array(
                    'open_id' => 'Openid'
                );
                break;
            case 'wx_pub_qr':

                $extra = array(
                    'product_id' => $orderNo
                );
                break;
            case 'yeepay_wap':
                $extra = array(
                    'product_category' => '1',
                    'identity_id' => 'your identity_id',
                    'identity_type' => 1,
                    'terminal_type' => 1,
                    'terminal_id' => 'your terminal_id',
                    'user_ua' => 'your user_ua',
                    'result_url' => $redirect_url
                );
                break;
            case 'jdpay_wap':
                $extra = array(
                    'success_url' => $redirect_url,
                    'fail_url' => 'http://www.yourdomain.com',
                    'token' => 'dsafadsfasdfadsjuyhfnhujkijunhaf'
                );
                break;
        }

        \Pingpp\Pingpp::setApiKey(modC('SECRET_KEY', '', 'Pingxx'));
        try {

            $assign = array(
                'subject' => $subject,
                'body' => $body,
                'amount' => $amount * 100,
                'order_no' => $orderNo,
                'currency' => 'cny',
                'extra' => $extra,
                'channel' => $channel,
                'client_ip' => $_SERVER['REMOTE_ADDR'],
                'app' => array('id' => modC('APP_ID', '', 'Pingxx'))
            );

            $ch = \Pingpp\Charge::create($assign);

            $this->assign('data', $ch);

            if ($channel == 'wx_pub_qr') {
                $savepath = '.' . __ROOT__ . "/Uploads/Picture/QRcode";
                $path = qrcode($ch->credential->wx_pub_qr, $orderNo . '.png', $savepath, false, 6);

               // $this->assign('qr', $path);
                $this->assign('qr', str_replace('./','/',$path));


                $this->assign($assign);
                $this->display('qrcode');
                exit;
            }


            $this->display();
            //echo $ch;
        } catch (\Pingpp\Error\Base $e) {
            header('Status: ' . $e->getHttpStatus());
            $this->error($e->getHttpBody(), U('pingxx/index/index'));
            //echo($e->getHttpBody());
        }
    }


}


